package com.jwthhealth_pub.old.report.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.LogUtil;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.report.model.resbody.EnviDescResp;
import com.jwthhealth_pub.old.report.view.adapter.EnviIndiAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

/**
 * 内环境指标说明
 * Created by liuzhen on 2017/10/23.
 */

public class IndicalEnvironmentActivity extends BaseActivity {

    private static String TAG = makeLogTag(IndicalEnvironmentActivity.class);

    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.rv_data)
    RecyclerView rvData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_indicator_environment);

        ButterKnife.bind(this);

        String id = getIntent().getStringExtra("id");
        if (id != null)
            getData(id);
    }

    private void getData(final String id) {

        Observable<EnviDescResp.DataBean> observable = Observable.create(new ObservableOnSubscribe<EnviDescResp.DataBean>() {
            @Override
            public void subscribe(ObservableEmitter<EnviDescResp.DataBean> e) throws Exception {
                Call<EnviDescResp> _call = ApiHelper.getEnvirStateDesc(id);
                EnviDescResp body = _call.execute().body();

                if (body == null) {
                    e.onError(new Exception("body is null"));
                    return;
                }

                String code = body.getCode();
                if (code == null) {
                    e.onError(new Exception(getString(R.string.common_net_error)));
                    return;
                }

                if (body.getCode().equals("0")) {
                    EnviDescResp.DataBean data = body.getData();
                    if (data != null)
                        e.onNext(body.getData());
                    else
                        e.onError(new Exception(getString(R.string.common_empty_data)));
                } else {
                    e.onError(new Exception(body.getMsg()));
                }
            }
        });

        Observer<EnviDescResp.DataBean> observer = new Observer<EnviDescResp.DataBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(EnviDescResp.DataBean dataBean) {
                String _title = dataBean.getTitle();
                if (_title != null) {
                    title.setTitle(_title);
                }

                List<EnviDescResp.DataBean.SonBean> _list = dataBean.getSon();
                if (_list != null && _list.size() > 0)
                    RenderingUI(_list);
            }

            @Override
            public void onError(Throwable e) {
                LogUtil.toast(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        };

        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
    }

    private void RenderingUI(List<EnviDescResp.DataBean.SonBean> data) {
        rvData.setLayoutManager(new LinearLayoutManager(this));
        rvData.setAdapter(new EnviIndiAdapter(this, data));
    }

    @Override
    protected void title() {
        title.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }
}
